﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace JxcLite;

public static class AppCore
{
    public static void AddApplicationWeb(this IServiceCollection services, Action<CoreOption> action)
    {
        services.AddApplicationCore();
        services.AddKnownWeb(option => SetOption(option, action));
    }

    public static void AddApplicationWin(this IServiceCollection services, Action<CoreOption> action)
    {
        services.AddApplicationCore();
        services.AddKnownWin(option => SetOption(option, action));
    }

    public static void UseApplication(this WebApplication app)
    {
        app.UseKnown();
    }

    private static void AddApplicationCore(this IServiceCollection services)
    {
        var assembly = typeof(AppCore).Assembly;
        services.AddServices(assembly);
        services.AddKnownCells();
    }

    private static void SetOption(CoreOption option, Action<CoreOption> action)
    {
        action?.Invoke(option);
        option.Database = db =>
        {
            var connString = "Data Source=JxcLite.db;";
            //var connString = builder.Configuration.GetSection("ConnString").Get<string>();
            //db.AddAccess<System.Data.OleDb.OleDbFactory>(connString);
            db.AddSQLite<Microsoft.Data.Sqlite.SqliteFactory>(connString);
            //db.AddSqlServer<Microsoft.Data.SqlClient.SqlClientFactory>(connString);
            //db.AddMySql<MySqlConnector.MySqlConnectorFactory>(connString);
            //db.AddPgSql<Npgsql.NpgsqlFactory>(connString);
            //db.AddDM<Dm.DmClientFactory>(connString);
            //db.SqlMonitor = c => Console.WriteLine($"{DateTime.Now:HH:mm:ss} {c}");
            //db.OperateMonitors.Add(info => Console.WriteLine(info.ToString()));
        };
        //option.ProductId = "Test";
        //option.CheckSystem = info => Result.Error("无效密钥，请重新授权！");
    }
}